package com.hubu.web;

import com.hubu.mapper.studentUserMapper;
import com.hubu.mapper.teacherUserMapper;
import com.hubu.pojo.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.List;

@WebServlet("/TeacherCourseDetail")
public class TeacherCourseDetail extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("detail get");
        System.out.println(req.getParameter("teacherId"));
        // TODO 这里需要根据id获取到 List<course_Information> selectTGradeByTeacherId(String id);传递参数

        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
                // 获取 Mapper 这里命名又点不规范
                teacherUserMapper t_userMapper = sqlSession.getMapper(teacherUserMapper.class);

                String userId =  req.getParameter("teacherId");
                String CourseId =  req.getParameter("courseId");

                List<student_single_grade> s_grade_inf = t_userMapper.selectTGradeByTeacherId(userId);
                req.setAttribute("s_grade_inf", s_grade_inf);
               // req.setAttribute("course_inf", course_inf);
                System.out.println(s_grade_inf);

//                System.out.println(course_inf);
                // 转发到 adminHome.jsp 页面
                RequestDispatcher requestDispatcher = req.getRequestDispatcher("teacherCourse.jsp");
                requestDispatcher.forward(req,resp);
            }
        } catch (Exception e) {
            // 在此处理异常，记录或适当地响应
            e.printStackTrace();  // 仅作演示之用，应替换为适当的日志记录或响应处理
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("msg put" + req.getParameter("newGrade"));
        System.out.println(req.getParameter("courseId"));
        System.out.println(req.getParameter("tid"));
        String sid = req.getParameter("sid");
        String cid = req.getParameter("courseId");
        String tid = req.getParameter("tid");
        String nGrade = req.getParameter("newGrade");
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

                teacherUserMapper t_userMapper = sqlSession.getMapper(teacherUserMapper.class);

                t_userMapper.updateByStudentIdAndCourseId(sid, cid, Integer.parseInt(nGrade));


                sqlSession.commit();
                PrintWriter writer=resp.getWriter();
                writer.println("<script type='text/javascript'>");
                writer.println("alert('操作成功！');");
//                writer.println("window.location='" + req.getContextPath() + "");
                writer.println("</script>");


                resp.sendRedirect(req.getContextPath() +"TeacherCourseDetail?teacherId="+ URLEncoder.encode(tid, "UTF-8")
                        + "&courseId="+URLEncoder.encode(cid, "UTF-8"));

            }
            catch (Exception e) {
                // 在此处理异常，记录或适当地响应
                //返回异常信息
                System.out.println(e);
            }
        } catch (Exception e) {
            // 在此处理异常，记录或适当地响应
            //返回异常信息
            System.out.println(e);
            PrintWriter writer=resp.getWriter();
            writer.println("<script type='text/javascript'>");
            writer.println("alert('操作失败！');");
//                writer.println("window.location='" + req.getContextPath() + "");
            writer.println("</script>");
            resp.sendRedirect(req.getContextPath() +"TeacherCourseDetail?teacherId="+ URLEncoder.encode(tid, "UTF-8")
                    + "&courseId="+URLEncoder.encode(cid, "UTF-8"));
        }

    }
}
